<?php
class Poling_con extends CI_Controller{
        function __construct() {
            parent::__construct();
            $this->load->library('wysiwyg');
        }
        function index(){
            //menampilkan halaman default
            //rencananya akan menampilkan tabel kelas yang ada dengan status belum (artinya baru ditawarkan)
            $this->tampil_pertanyaan();
        }

        function tampil_pertanyaan()
        {
            // menampilkan daftar pertanyaan
            $query = $this->db->get('pertanyaan');
            $row=$query->result_array();
            $data2['data']=$row;
//            print_r($row);
//            die();
            $data['script']=$this->wysiwyg->loadScript();   //form akan menggunakan textarea WYSIWYG
            $data['title']="Tampil Pertanyaan";
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/pertanyaan_display',$data2);
            $this->load->view('footer_view');
        }

        function tambah_pertanyaan(){
            //nambah pertanyaan
            // menambah pertanyaan
            $nama = $this->input->get('nama');
            $tipe = $this->input->get('tipe');
            $jumlah = $this->input->get('jumlah');
            $data2['nama'] = $nama;
            $data2['tipe'] = $tipe;
            $data2['jumlah'] = $jumlah;
            $data2['pilihan'] = null;
            $data2['mode'] = null;
            $data['script']=$this->wysiwyg->loadScript();   //form akan menggunakan textarea WYSIWYG
            $data['title']="Tambah Pertanyaan";
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/pertanyaan_form',$data2);
                    
            $this->load->view('footer_view');
        }
        
        function simpan_tambah_pertanyaan(){
            //melakukan insert database pertanyaan dan daftar pilihan
            $nama = $this->input->post('nama');
            $tipe = $this->input->post('tipe');
            $jawaban = $this->input->post('jawaban');
//            die ($nama.":".$tipe);
            $this->db->insert('pertanyaan', array('ISI_PERTANYAAN' => $nama, 'TIPE_PERTANYAAN' => $tipe));
            if($tipe!='essay')
                {
                    $query = $this->db->query('SELECT MAX(ID_PERTANYAAN) AS id FROM pertanyaan');
                    $row = $query->result_array();
//                    print_r($row);
//                    die();
                    for ($i=0;$i<count($jawaban);$i++)
                    {
                        $this->db->insert('daftar_pilihan', array('ID_PERTANYAAN' => $row[0]['id'], 'ISI_PILIHAN' => $jawaban[$i]));
                    }
                }
            redirect(base_url() . 'admin/poling_con/');
        }

        
        function edit_pertanyaan($id){
            //melakukan edit pertanyaan
            $query = $this->db->get_where('pertanyaan',array ('ID_PERTANYAAN'=>$id));
            $row = $query->result_array();
            $data2['nama']=$row[0]['ISI_PERTANYAAN'];
            $data2['tipe'] = $row[0]['TIPE_PERTANYAAN'];
            $data['script'] = $this->wysiwyg->loadScript();   //form akan menggunakan textarea WYSIWYG
            $data['title']="Edit Pertanyaan";
            $query = $this->db->query('SELECT * FROM daftar_pilihan WHERE ID_PERTANYAAN = '.$id);
            $row = $query->result_array();
            $data2['pilihan'] = $row;
            $data2['mode'] = "edit";
            $data2['id'] = $id;
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/pertanyaan_form',$data2);
            $this->load->view('footer_view');
        }

        function simpan_edit_pertanyaan($id)
        {
            //simpan edit pertanyaan
            $data['script'] = $this->wysiwyg->loadScript();   //form akan menggunakan textarea WYSIWYG
            $data['title']="Edit Pertanyaan";
            $this->load->view('admin/admin_header_view', $data);
            $jawaban = $this->input->post('jawaban');
            $nama = $this->input->post('nama');
            $tipe = $this->input->post('tipe');
            $query = $this->db->query('UPDATE pertanyaan SET ISI_PERTANYAAN="'. $nama .'", TIPE_PERTANYAAN="'. $tipe .'" WHERE ID_PERTANYAAN = '.$id);
            $this->db->delete('daftar_pilihan', array('ID_PERTANYAAN' => $id));
            if ($tipe!="essay")
                for($i=0;$i<count($jawaban);$i++)
            $this->db->insert('daftar_pilihan', array('ID_PERTANYAAN' => $id,'ISI_PILIHAN' => $jawaban[$i]));
            redirect(base_url().'admin/poling_con/tampil_pertanyaan');
        }
        function hapus_pertanyaan($id){
            //menghapus pertanyaan dengan id=$id
            $this->db->delete('pertanyaan', array('ID_PERTANYAAN' => $id));
            $this->db->delete('daftar_pilihan', array('ID_PERTANYAAN' => $id));
            redirect(base_url().'admin/poling_con/tampil_pertanyaan');
        }

    }

?>
